﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtJSExtender.Controls
{
    public class GridDataStore
    {
        private int pageSize = 25;
        /// <summary>
        /// 每页的数据行数，默认为每页显示25行
        /// </summary>
        public int PageSize
        {
            get
            {
                return pageSize;
            }
            set
            {
                pageSize = value;
            }
        }

        private int totalCount = 0;
        /// <summary>
        /// 数据的总行数
        /// </summary>
        public int TotalCount
        {
            get
            {
                return totalCount;
            }
            set
            {
                totalCount = value;
            }
        }

        private int currentPage = 1;
        /// <summary>
        /// 当前页码，从1开始，默认为1
        /// </summary>
        public int CurrentPage
        {
            get
            {
                return currentPage;
            }
            set
            {
                currentPage = value;
            }
        }

        private List<Dictionary<string, object>> data = null;
        /// <summary>
        /// 数据
        /// </summary>
        public List<Dictionary<string, object>> Data
        {
            get
            {
                return data;
            }
            set
            {
                data = value;
            }
        }

        private string dataUrl = null;
        /// <summary>
        /// 获取数据的地址
        /// </summary>
        public string DataUrl
        {
            get
            {
                return dataUrl;
            }
            set
            {
                dataUrl = value;
            }
        }

        #region 数据处理

        public void BindData(object source)
        {
            IEnumerable enumerable = getResolvedDataSource(source);
            if (enumerable == null)
                throw new ArgumentException("source不是有效的数据源");
            IEnumerator enumerator = enumerable.GetEnumerator();

            List<object> dataItems = new List<object>();
            while (enumerator.MoveNext())
            {
                dataItems.Add(enumerator.Current);
            }

            //此处耗费性能！！！
            var jsonString = Util.SerializeToJson(dataItems);
            Data = Util.DeserializeJson<List<Dictionary<string, object>>>(jsonString);
        }

        private IEnumerable getResolvedDataSource(object source)
        {
            if (source is DataSet)
                return (IEnumerable)(((DataSet)source).Tables[0].Rows);
            else if (source is DataTable)
                return (IEnumerable)(((DataTable)source).Rows);
            else if (source is IEnumerable)
                return (IEnumerable)source;
            else if (source is IList)
                return (IEnumerable)source;
            else
                return null;
        }

        #endregion
    }
}
